package internal.generator;

import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

/**
 * 
 * @author badqiu
 * @email badqiu(a)gmail.com
 */
public class StringTemplate {

	private String str;
	private Map<String, Object> params;
	
	public StringTemplate(String str, Map<String, Object> params) {
		this.str = str;
		this.params = params;
	}

	public String toString() {
		String  result = str;
		result = result.replace("$$", "%%");
		for(Iterator<Entry<String, Object>> it = params.entrySet().iterator(); it.hasNext(); ) {
			Map.Entry<String, Object> entry = it.next();
			String key = entry.getKey();
			Object value = entry.getValue();
			String strValue = value == null ? "" : value.toString();
//			result = StringHelper.replace(result, "${"+key+"}", strValue);
			result = result.replace("${"+key+"}", strValue);
		}
		result = result.replace("%%", "$");
		return result;
	}
	
	
}
